package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.MobileSetmealDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.untils.FreemarkUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService{

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private MobileSetmealDao mobileSetmealDao;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    /*
    * 获取配置文件属性值*/
    @Value("${out_put_path}")
    private String out_put_path;

    @Override
    public PageResult selectAllByName(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> setmeals=null;
        try {
            setmeals = setmealDao.SelectAllByName(queryString);
        } catch (Exception e) {
            e.printStackTrace();
        }
       return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }

    @Override
    public Result add(Setmeal setmeal,Integer[] integers) {
        try {
            setmealDao.addSetmeal(setmeal);
            for (Integer gid : integers) {
                setmealDao.addSAndG(setmeal.getId(),gid);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
        //获取缓存中的静态html
        Set<String> id = jedisPool.getResource().sdiff("id");
        List<Setmeal> setmeal1 = mobileSetmealDao.getSetmeal();
        FreemarkUtils.getData(freeMarkerConfigurer,out_put_path,jedisPool);
        if (id==null||id.size()==0){
            FreemarkUtils.generateMobileStaticHtml(setmeal1);
        }else {
            Setmeal st = mobileSetmealDao.findById(setmeal.getId());
            List<Setmeal> setmealList = new ArrayList<>();
            setmealList.add(st);
            FreemarkUtils.generateMobileSetmealDetailHtml(setmealList);
            FreemarkUtils.generateMobileSetmealListHtml(setmeal1);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @Override
    public Result update(Setmeal setmeal, Integer[] integers) {
        try {
            setmealDao.updateSetmeal(setmeal);
            if (integers!=null&&integers.length>0){
                setmealDao.deleteById(setmeal.getId());
                for (Integer gid : integers) {
                    setmealDao.addSAndG(setmeal.getId(),gid);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑套餐失败");
        }
        Set<String> id = jedisPool.getResource().sdiff("id");
        List<Setmeal> setmeal1 = mobileSetmealDao.getSetmeal();
        FreemarkUtils.getData(freeMarkerConfigurer,out_put_path,jedisPool);
        if (id!=null&&id.size()>0){
            Setmeal st = mobileSetmealDao.findById(setmeal.getId());
            List<Setmeal> setmealList = new ArrayList<>();
            setmealList.add(st);
            FreemarkUtils.generateMobileSetmealListHtml(setmeal1);
            FreemarkUtils.generateMobileSetmealDetailHtml(setmealList);
        }else {
            FreemarkUtils.generateMobileStaticHtml(setmeal1);
        }
        return new Result(true,"编辑套餐成功");
    }

    @Override
    public Result delete(int id) {
        try {
            setmealDao.deleteById(id);
            setmealDao.deleteOrderById(id);
            setmealDao.deleteOneById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
        Set<String> ids = jedisPool.getResource().sdiff("id");
        List<Setmeal> setmeal1 = mobileSetmealDao.getSetmeal();
        FreemarkUtils.getData(freeMarkerConfigurer,out_put_path,jedisPool);
        FreemarkUtils.generateMobileSetmealListHtml(setmeal1);
        if (ids!=null&&ids.size()>0){
            for (String s : ids) {
                if (Integer.parseInt(s)==id){
                    jedisPool.getResource().srem("id",s);
                }
            }
        }else {
            FreemarkUtils.generateMobileStaticHtml(setmeal1);
        }
        //generateMobileStaticHtml();
        return new Result(true,"删除成功");
    }



}
